var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(a) {
    return a && a.__esModule ? a : {
        default: a
    };
}

var that, app = getApp();
let interstitialAd = null;

Page({
    data: {
        setTimeself: "",
        time: 0
    },
    onLoad: function() {
        that = this;
    },
    onReady: function() {
        var a = {
            action: "myBargain",
            contr: "my",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(a, function(a) {
            that.setData(a.info), that.clearTime(), that.countdown();
        }), wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#000000"
        });
    },
    onShow: function() {
        if (wx.createInterstitialAd) {
            interstitialAd = wx.createInterstitialAd({
              adUnitId: 'adunit-42e141bfcfdcc315'
            })
            interstitialAd.onLoad(() => {})
            interstitialAd.onError((err) => {})
            interstitialAd.onClose(() => {})
          };
          
          // 在适合的场景显示插屏广告
          if (interstitialAd) {
            interstitialAd.show().catch((err) => {
              console.error(err)
            })
          };
    },
    gotoBargain: function(a) {
        var t = a.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/bh_bargain/pages/bargain/bargain?id=" + t
        });
    },
    clearTime: function() {
        clearTimeout(that.data.setTimeself);
    },
    countdown: function() {
        if (null != this.data.bargain_goods && this.data.bargain_goods) {
            for (var a in this.data.time++, this.data.bargain_goods) {
                var t = parseInt(that.data.bargain_goods[a].surplus_time) - this.data.time;
                if (that.data.bargain_goods[a].d = "00", that.data.bargain_goods[a].h = "00", that.data.bargain_goods[a].m = "00", 
                that.data.bargain_goods[a].s = "00", 0 < t && 1 == that.data.bargain_goods[a].status) {
                    var o = parseInt(t / 60 / 60 / 24, 10), e = Math.floor(t / 60 / 60 % 24), i = Math.floor(t / 60 % 60), n = Math.floor(t % 60);
                    e < 10 && (e = "0" + e), o < 10 && (o = "0" + o), i < 10 && (i = "0" + i), n < 10 && (n = "0" + n), 
                    that.data.bargain_goods[a].d = o, that.data.bargain_goods[a].h = e, that.data.bargain_goods[a].m = i, 
                    that.data.bargain_goods[a].s = n;
                }
            }
            that.data.setTimeself = setTimeout(function() {
                that.setData({
                    bargain_goods: that.data.bargain_goods
                }), that.countdown();
            }, 1e3);
        } else clearTimeout(that.data.setTimeself);
    },
    onUnload: function() {
        this.clearTime();
    },
    onShareAppMessage: function() {
        return {
            title: this.data.share.text,
            imageUrl: this.data.share.images,
            path: "bh_bargain/pages/index/index?parent_id=" + this.data.share.member_id
        };
    }
});